// agentManage.js
const app = getApp()
var util = require("../../../utils/util.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: '0',
    tablineStyle: '',
    search:{
      searchAddShow: false,
      BtnTxt:'添加代理',
      searchTip: '可输入ID、姓名等',
      searchValue: '',
    },

    list:[],
    pagenum:1
  },
  // 切换tab
  tabChoose: function (e) {
    var that = this;
    var cur = e.target.dataset.current;
    var level = cur == '0' ? '1' : '2';
    if (that.data.currentTab == cur) { return false; }
    else {
      that.setData({
        currentTab: cur,
        pagenum: 1,
        list: []
      })
    }
    that.tabLine();
    that.manageList(level);

  },
  // 切换tab滑块动画
  tabLine: function () {
    var query = wx.createSelectorQuery();
    var that = this;
    var currentTab = that.data.currentTab;
    query.select('.tab').boundingClientRect(function (rect) {
      that.setData({
        tablineStyle: "width:" + rect.width / 2 + "px;transform: translateX(" + rect.width * currentTab / 2 + "px);"
      })
    }).exec();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var level = this.data.currentTab == '0' ? '1' : '2';
    this.tabLine();
    this.manageList(level);
    wx.stopPullDownRefresh()
  },
  searchChange:function(event){
    var detail = 'search.searchValue'
    this.setData({
      [detail]: event.detail
    })
  },
  manageList:function(level){
    var that = this;
    var data = { 
      page: that.data.pagenum,
      value: that.data.search.searchValue,
      level:level
      };
    app.post("agent/allAgent", data).then((res) => {
      if (res.code == 200) {
        var list = that.data.list;
        var dataArr = res.list;
        list = list.concat(dataArr)
        that.setData({
          list:list
        })
      }
    }).catch((errMsg) => {
      console.log(errMsg);//错误提示信息
    })
  },
  onSearch:function() {
    var level = this.data.currentTab == '0' ? '1' : '2';
    this.setData({ list: [] })
    this.manageList(level);
  },
  ToDetailTap:function(e){
    var index = e.currentTarget.dataset.index;
    var that = this;
    var list = that.data.list;
    var level = that.data.currentTab == '0' ? '1' : '2';
    wx.navigateTo({
      url: '../../../pages/proxySubAgDetail/proxySubAgDetail?detail=' + JSON.stringify(list[index]) + '&level='+level,
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      list: [],
      pagenum: 1,
      ['search.searchValue']: ''
    })
    this.onLoad();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var pagenum = that.data.pagenum + 1; //获取当前页数并+1
    var level = this.data.currentTab == '0' ? '1' : '2';
    that.setData({
      pagenum: pagenum, //更新当前页数
    })
    that.manageList(level);//重新调用请求获取下一页数据
  }
})